<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 顶部Logo -->
      <div class="login-header">
        <h1 class="title">智慧养老平台</h1>
        <p class="subtitle">关爱长者，智慧生活</p>
      </div>

      <!-- 登录方式切换 -->
      <div class="login-tabs">
        <button 
          class="tab-button active"
          @click="loginType = 'account'"
        >
          账号登录
        </button>
        <button 
          class="tab-button"
          @click="loginType = 'email'"
        >
          邮箱登录
        </button>
      </div>

      <!-- 登录表单 -->
      <form class="login-form" @submit.prevent="handleLogin">
        <!-- 账号登录表单 -->
        <div v-if="loginType === 'account'" class="form-content">
          <div class="form-group">
            <label for="account" class="form-label">账号</label>
            <div class="input-wrapper">
              <i class="fa fa-user input-icon"></i>
              <input 
                type="text" 
                id="account" 
                v-model="accountForm.account" 
                class="form-input"
                placeholder="请输入账号"
                required
              >
            </div>
          </div>
          
          <div class="form-group">
            <label for="password" class="form-label">密码</label>
            <div class="input-wrapper">
              <i class="fa fa-lock input-icon"></i>
              <input 
                type="password" 
                id="password" 
                v-model="accountForm.password" 
                class="form-input"
                placeholder="请输入密码"
                required
              >
              <button 
                type="button" 
                class="toggle-password"
                @click="togglePasswordVisibility"
              >
                <i :class="showPassword ? 'fa fa-eye-slash' : 'fa fa-eye'"></i>
              </button>
            </div>
          </div>
        </div>

        <!-- 手机号登录表单 -->
        <div v-else class="form-content">
          <div class="form-group">
            <label for="email" class="form-label">邮箱号</label>
            <div class="input-wrapper">
              <i class="fa fa-phone input-icon"></i>
              <input 
                type="text" 
                id="email" 
                v-model="phoneForm.email" 
                class="form-input"
                placeholder="请输入邮箱号"
                required
              >
            </div>
          </div>
          
          <div class="form-group">
            <label for="code" class="form-label">验证码</label>
            <div class="input-wrapper">
              <i class="fa fa-key input-icon"></i>
              <input 
                type="text" 
                id="code" 
                v-model="phoneForm.code" 
                class="form-input"
                placeholder="请输入验证码"
                maxlength="6"
                required
              >
              <button 
                type="button" 
                class="get-code"
                :disabled="countdown > 0"
                @click="sendCode"
              >
                {{ countdown > 0 ? `${countdown}s后重新发送` : '获取验证码' }}
              </button>
            </div>
          </div>
        </div>

        <!-- 记住密码和忘记密码 -->
        <div class="remember-forgot">
          <div class="remember-me">
            <!-- <input 
              id="remember-me" 
              type="checkbox" 
              class="remember-checkbox"
              v-model="rememberMe"
            > -->
            <!-- <label for="remember-me" class="remember-label">
              记住密码
            </label> -->
          </div>
          <div class="forgot-password">
            <!-- <a href="#" class="forgot-link">
              忘记密码?
            </a> -->
          </div>
        </div>

        <!-- 登录按钮 -->
        <button 
          type="submit" 
          class="login-button"
        >
          登录
        </button>

       

        <!-- 注册链接 -->
        <div class="register">
          <p class="register-text">
            还没有账号? 
            <a href="/Registered" class="register-link">
              立即注册
            </a>
          </p>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { login,authCode,emailLogin} from '@/api/login'; 
import { useUserStore } from '@/stores/userStore';
import router from '@/router';
import { number } from 'echarts';
// 登录类型：account(账号密码登录) | phone(手机号登录)
const userStore = useUserStore()
const loginType = ref('account');

// 账号密码登录表单数据
const accountForm = reactive({
  account: '',
  password: ''
});

// 邮箱登录表单数据
const phoneForm = reactive({
  email: '',
  code: ''
});

// 记住密码选项
const rememberMe = ref(false);

// 密码可见性控制
const showPassword = ref(false);

// 验证码倒计时
const countdown = ref(0);

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};

// 发送验证码
const sendCode = () => {
  if (!phoneForm.email) {
    alert('请输入邮箱号');
    return;
  }

  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(phoneForm.email)) {
    alert('请输入正确的邮箱');
    return;
  }

  // 发送验证码API
  authCode({ email: phoneForm.email })
    .then(response => {
     

      // 开始60秒倒计时
      countdown.value = 60;
      const timer = setInterval(() => {
        countdown.value--;
        if (countdown.value <= 0) {
          clearInterval(timer);
        }
      }, 1000);
    })
    .catch(error => {
      alert('验证码发送失败，请稍后再试');
    });
  
};

// 处理登录
const handleLogin = () => {
  if (loginType.value === 'account') {
    // 账号密码登录验证
    if (!accountForm.account) {
      alert('请输入账号');
      return;
    }
    
    if (!accountForm.password) {
      alert('请输入密码');
      return;
    }
    login({account: accountForm.account, password: accountForm.password})
      .then(response => {
        // 保存用户信息和token到全局状态
        userStore.loginSuccess(response.data)
        router.push("/BigScreen")
      })
      .catch(error => {
        console.error('登录失败:', error);
        alert('登录失败，请检查账号或密码');
      });
      
    
  
  } else {
    // 邮箱登录验证
    if (!phoneForm.email) {
      alert('请输入邮箱');
      return;
    }

    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(phoneForm.email)) {
      alert('请输入正确的邮箱');
      return;
    }
    
    if (!phoneForm.code) {
      alert('请输入验证码');
      return;
    }
    emailLogin({ email: phoneForm.email, code: phoneForm.code })
      .then(response => {
  
        // 保存用户信息和token到全局状态
        userStore.loginSuccess(response.data)
       
        router.push("/BigScreen")
      })
      .catch(error => {
        console.error('邮箱登录失败:', error);
        alert('邮箱登录失败' + response.data.message);
      });
   
  }
};

// 页面加载时检查是否有记住的密码
onMounted(() => {
  const savedAccount = localStorage.getItem('savedAccount');
  const savedPassword = localStorage.getItem('savedPassword');
  
  if (savedAccount && savedPassword) {
    accountForm.account = savedAccount;
    accountForm.password = savedPassword;
    rememberMe.value = true;
  }
});

// 监听记住密码状态变化
watch(rememberMe, (newValue) => {
  if (newValue) {
    localStorage.setItem('savedAccount', accountForm.account);
    localStorage.setItem('savedPassword', accountForm.password);
  } else {
    localStorage.removeItem('savedAccount');
    localStorage.removeItem('savedPassword');
  }
});
</script>

<style scoped>
/* 导入字体图标 */
@import url('https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css');

/* 基础样式 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
}

/* 登录容器 */
.login-container {
  min-height: 100vh;
  background: linear-gradient(to bottom right, #1a56db, #3b82f6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* 登录卡片 */
.login-card {
  width: 100%;
  max-width: 400px;
  background-color: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

/* 登录头部 */
.login-header {
  background-color: #1e40af;
  padding: 40px 20px;
  text-align: center;
}

.title {
  color: white;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 10px;
}

.subtitle {
  color: #bfdbfe;
  font-size: 16px;
}

/* 登录选项卡 */
.login-tabs {
  display: flex;
  border-bottom: 1px solid #e2e8f0;
}

.tab-button {
  flex: 1;
  padding: 15px 0;
  text-align: center;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 2px solid transparent;
}

.tab-button.active {
  color: #1e40af;
  border-bottom-color: #1e40af;
}

.tab-button:not(.active) {
  color: #64748b;
}

.tab-button:not(.active):hover {
  color: #334155;
}

/* 登录表单 */
.login-form {
  padding: 40px 30px;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.form-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  color: #334155;
  font-size: 14px;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
}

.form-input {
  width: 100%;
  padding: 12px 12px 12px 40px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: #1e40af;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

.toggle-password {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: none;
  color: #94a3b8;
  cursor: pointer;
  transition: color 0.3s ease;
}

.toggle-password:hover {
  color: #475569;
}

.get-code {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: none;
  color: #1e40af;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s ease;
}

.get-code:hover:not(:disabled) {
  color: #0c4a6e;
}

.get-code:disabled {
  color: #94a3b8;
  cursor: not-allowed;
}

/* 记住密码和忘记密码 */
.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
}

.remember-checkbox {
  width: 16px;
  height: 16px;
  accent-color: #1e40af;
}

.remember-label {
  color: #475569;
  font-size: 14px;
}

.forgot-password {
  font-size: 14px;
}

.forgot-link {
  color: #1e40af;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #0c4a6e;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  padding: 14px 0;
  background-color: #1e40af;
  color: white;
  font-size: 16px;
  font-weight: 500;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-button:hover {
  background-color: #172554;
  transform: translateY(-2px);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.login-button:active {
  transform: translateY(0);
  box-shadow: none;
}

/* 其他登录方式 */
.other-login {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.other-text {
  color: #64748b;
  font-size: 14px;
}

.other-icons {
  display: flex;
  gap: 30px;
}

.icon-button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #64748b;
  border: none;
  background: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.icon-button:hover {
  color: #1e40af;
  transform: scale(1.1);
}

.icon-button i {
  font-size: 20px;
}

/* 注册链接 */
.register {
  text-align: center;
  font-size: 14px;
  color: #64748b;
}

.register-link {
  color: #1e40af;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #0c4a6e;
}

/* 动画效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>    